<?php
class Login extends Controller {
  function __construct() {
    parent::Controller();
  }

  function index() {
    $data['header_title'] = 'pFMPortal';
    $data['main_content'] = 'login/loginview';
    $data['style'] = 'default';
    $this->load->view('includes/template', $data);
  }

  function signup() {
    $data['header_title'] = 'pFMPortal Signup';
    $data['main_content'] = 'signup/signupview';
    $data['style'] = 'default';
    $this->load->view('includes/template', $data);
  }

  function logout() {
    $this->session->sess_destroy();
    log_message('debug','Session was destroyed!');
    redirect('login');
  }

  function validate() {
    $this->load->model('user/user','',TRUE);
    $this->load->model('crudrecord','crud',TRUE);

    $validation = $this->user->validateUser();

    if($validation !== NULL) { // if the user's credentials validated...
      $data = array('username' => $validation['username'],
          'password' => $validation['password'],
          'nickName' => $validation['nickName'],
          'clubId' => $validation['clubId'],
          'isVIP' => $validation['isVIP'],
          'lastLogin' => $validation['lastLogin'],
          'is_logged_in' => TRUE
      );
      $this->session->set_userdata($data);
      $this->load->helper('schema');

      $schemas = loadSchemasFrom('schema/account/');

      $tAccountFile = downloadOwnAccountWith($data['username'],$data['password']);
      $tAccountDoc = loadXMLFromFile($tAccountFile);

      foreach ($schemas as $schema) {
        $tData = array();
        $tableName = getTableNameFrom($schema);
        $where = getFilterFrom($schema);
        $where[COL_CLUBID] = $data['clubId'];

        $tData = parseXMLDoc($schema,$tAccountDoc);
        if ($tData == FALSE) {
          continue;
        }
        $tId = $this->crud->getId($tableName,$where);
        $this->crud->update($tableName,$tId[COL_ID],$tData);
      }

      redirect('site/members_area');
    } else {
      redirect('login');
    }
  }

  function create_member() {
    $this->load->library('form_validation');
    $this->load->model('user/user','',TRUE);

    // field name, error message, validation rules
    $this->form_validation->set_rules('first_name', 'Name', 'trim|required|max_length[32]');
    $this->form_validation->set_rules('nick_name', 'Nick Name', 'trim|max_length[32]');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[32]');
    $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email|max_length[256]');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


    if($this->form_validation->run() == FALSE) {
      $data['header_title'] = 'pFMPortal Signup Error';
      $data['style'] = 'default';
      $this->load->view('includes/header',$data);
      $this->load->view('signup/signupview');
      $this->load->view('includes/footer');
    } else {
      $data['header_title'] = 'pFMPortal Signup';
      $this->load->model('account/account','',TRUE);
      $this->load->library('freekick');

      $tDownload = $this->freekick->downloadOwnAccountWith($this->input->post('username'),encrypt($this->input->post('password')));
      if (!$tDownload) {
        redirect('login/signup');
      }

      $tLoad = $this->freekick->loadFromFile('schema/account/account.xml',$tDownload,TRUE);
      if (!$tLoad) {
        redirect('login/signup');
      }

      $tInsert = $this->crud->create(ACCOUNT_TABLENAME,$tLoad);
      if (!$tInsert) {
        redirect('login/signup');
      }

      $query = $this->user->create($tInsert);

      if($query) {
        redirect('login');
      } else {
        redirect('login/signup');
      }
    }

  }
}
?>